const axios = require("../../demo/simple-post/node_modules/axios")

openGate = function (no = 0) {
  const URL = 'https://louxiaoyuapi.etor.vip/api/doorway/v1/device/OpenDoorWX'
  const OPENID = 'oVUUc5NKao2SSq7L24notLR_aNB8'
  const DEVICE_NO_LIST = [
    'BCDDC2D71F24',
    '30AEA4CBD1C4',
    '30AEA4CBD2D8',
    '30AEA4C592D4'
  ]
  const NEED_OPEN_GATE = no > 0 ? [DEVICE_NO_LIST[no - 1]] : DEVICE_NO_LIST.slice()
  NEED_OPEN_GATE.forEach((deviceNo, i) => {
    const config = {
      method: 'post',
      url: URL,
      headers: {
        'content-type': 'application/json'
      },
      data: {
        openid: OPENID,
        deviceNo: deviceNo
      }
    }
    axios(config).then(res => {
      if (res.data.Code === 10000) {
        console.log(no === 0 ? `${i+1}号门开启成功` : `${no}号门开启成功`)
      } else {
        throw new Error(res.data.Message)
      }
    }).catch(err => {
      console.log(`${no}号门开启失败`, err.message)
    })
  })
  return {
    message: `${no ? no + '号门' : '所有门'}开启成功`
  }
}

// openGate(4)
